package com.ruoyi.system.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.TbHotel;
import com.ruoyi.system.mapper.TbHotelTypeMapper;
import com.ruoyi.system.domain.TbHotelType;
import com.ruoyi.system.service.ITbHotelTypeService;

/**
 * 酒店类型Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-07-11
 */
@Service
public class TbHotelTypeServiceImpl implements ITbHotelTypeService 
{
    @Autowired
    private TbHotelTypeMapper tbHotelTypeMapper;

    /**
     * 查询酒店类型
     * 
     * @param id 酒店类型主键
     * @return 酒店类型
     */
    @Override
    public TbHotelType selectTbHotelTypeById(Long id)
    {
        return tbHotelTypeMapper.selectTbHotelTypeById(id);
    }

    /**
     * 查询酒店类型列表
     * 
     * @param tbHotelType 酒店类型
     * @return 酒店类型
     */
    @Override
    public List<TbHotelType> selectTbHotelTypeList(TbHotelType tbHotelType)
    {
        return tbHotelTypeMapper.selectTbHotelTypeList(tbHotelType);
    }

    /**
     * 新增酒店类型
     * 
     * @param tbHotelType 酒店类型
     * @return 结果
     */
    @Transactional
    @Override
    public int insertTbHotelType(TbHotelType tbHotelType)
    {
        // 只保留icon字段的第一个url
        if (tbHotelType.getIcon() != null && tbHotelType.getIcon().contains(",")) {
            tbHotelType.setIcon(tbHotelType.getIcon().split(",")[0]);
        }
        int rows = tbHotelTypeMapper.insertTbHotelType(tbHotelType);
        insertTbHotel(tbHotelType);
        return rows;
    }

    /**
     * 修改酒店类型
     * 
     * @param tbHotelType 酒店类型
     * @return 结果
     */
    @Transactional
    @Override
    public int updateTbHotelType(TbHotelType tbHotelType)
    {
        // 只保留icon字段的第一个url
        if (tbHotelType.getIcon() != null && tbHotelType.getIcon().contains(",")) {
            tbHotelType.setIcon(tbHotelType.getIcon().split(",")[0]);
        }
        tbHotelTypeMapper.deleteTbHotelByTypeId(tbHotelType.getId());
        insertTbHotel(tbHotelType);
        return tbHotelTypeMapper.updateTbHotelType(tbHotelType);
    }

    /**
     * 批量删除酒店类型
     * 
     * @param ids 需要删除的酒店类型主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTbHotelTypeByIds(Long[] ids)
    {
        tbHotelTypeMapper.deleteTbHotelByTypeIds(ids);
        return tbHotelTypeMapper.deleteTbHotelTypeByIds(ids);
    }

    /**
     * 删除酒店类型信息
     * 
     * @param id 酒店类型主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTbHotelTypeById(Long id)
    {
        tbHotelTypeMapper.deleteTbHotelByTypeId(id);
        return tbHotelTypeMapper.deleteTbHotelTypeById(id);
    }

    /**
     * 新增酒店管理信息
     * 
     * @param tbHotelType 酒店类型对象
     */
    public void insertTbHotel(TbHotelType tbHotelType)
    {
        List<TbHotel> tbHotelList = tbHotelType.getTbHotelList();
        Long id = tbHotelType.getId();
        if (StringUtils.isNotNull(tbHotelList))
        {
            List<TbHotel> list = new ArrayList<TbHotel>();
            for (TbHotel tbHotel : tbHotelList)
            {
                tbHotel.setTypeId(id);
                list.add(tbHotel);
            }
            if (list.size() > 0)
            {
                tbHotelTypeMapper.batchTbHotel(list);
            }
        }
    }
}
